
# FIGURE 4 #####################################################################

# Interaction data frames for two-way interaction heatmap 

# --- combine interaction data frames:
heat<-rbind(inter_A_keep, inter_N_keep, inter_O_keep,inter_C_keep,inter_E_keep, 
            inter_selfest_keep, 
            inter_gender_keep, inter_age_keep, inter_edu_keep)

# --- reshape data for plotting with neg_combi and pos_combi
heat_long <- heat %>%
  pivot_longer(
    cols = c(combi_neg, combi_pos),  # Focus on these two combination columns
    names_to = "combination_type",
    values_to = "combination"
  ) %>%
  filter(!is.na(EMM)) %>%  # Remove NA rows
  mutate(
    Interested = ifelse(InterestedNum == 0, "Uninterested", "Interested"),
    Interested = factor(Interested, levels = c("Uninterested", "Interested")),
    # Create formatted CI label
    CI_label = sprintf("[%.2f, %.2f]", CI_lower, CI_upper))


# --- filter to retain only the specified combinations:
combinations_to_keep <- c("0 0.045", "0 0.739", "0 -1.355", "0 -1.035", 
                          "1 -0.945", "1 -0.25", "1 1.215", "1 1.444")

heat_long_filtered <- heat_long %>%
  filter(combination %in% combinations_to_keep) %>%
  mutate(
    combination_type = ifelse(combination_type == "combi_neg", "Negative ID", "Positive ID")
  )

# --- heatmap plotting:
plot_heat_META_CI_two<-heat_long_filtered %>%
  distinct(DV, combination, Interested, .keep_all = TRUE) %>%
  mutate(
    combination = fct_relevel(
      combination,
      "0 0.045", "0 0.739", "0 -1.355", "0 -1.035",
      "1 -0.945", "1 -0.25", "1 1.215", "1 1.444"
    ),
    DV = fct_relevel(
      DV,"Agreeableness", "Neuroticism", "Openness", "Conscientiousness", "Extroversion", "Self-esteem", "Female", "Age", "Education"),
    CI_label = paste0(
      "[", format(round(CI_lower, 2), nsmall = 2), 
      ", ", format(round(CI_upper, 2), nsmall = 2), "]"
    )
  ) %>%
  ggplot(aes(
    x = factor(
      combination,
      levels = c(
        "0 0.045", "0 0.739", "0 -1.355", "0 -1.035",
        "1 -0.945", "1 -0.25", "1 1.215", "1 1.444"
      ),
      labels = c("HighPos", "HighNeg", "LowPos", "LowNeg",
                 "lowNeg", "lowPos", "highNeg", "highPos")
    ),
    y = DV,
    fill = EMM
  )) + scale_x_discrete(
    labels = c(
      "HighPos" = "HighPos",
      "HighNeg" = "HighNeg",
      "LowPos"  = "LowPos",
      "LowNeg"  = "LowNeg",
      "lowNeg"  = "LowNeg",
      "lowPos"  = "LowPos",
      "highNeg" = "HighNeg",
      "highPos" = "HighPos"
    )
  )+
  geom_tile(width = 0.9, height = 0.8, lwd = 3) +
  geom_text(aes(label = format(round(EMM, 2), nsmall = 2)), color = "white", size = 5, vjust = -0.3, fontface="bold") +
  geom_text(aes(label = CI_label), color = "white", size = 4, vjust = 1.3) +
  scale_fill_viridis(name = "", option = "E", labels = scales::number_format(accuracy = 0.01)) +
  facet_grid(~ Interested, scales = "free_x") +
  theme_minimal() +
  labs(title = "", x = "", y = "") +
  theme(
    text = element_text(family = "Times New Roman"),
    legend.position = "bottom",
    legend.key.width = unit(3, "cm"),
    legend.title = element_text(size = 12, vjust = 1),
    legend.text = element_text(size = 12),
    axis.text.y = element_text(size = 16, vjust = 0, hjust = 0.005),
    axis.text.x = element_text(size = 12, vjust = 3, hjust = 0.4),
    plot.title = element_text(hjust = 0, size = 20),
    axis.ticks = element_blank(),
    strip.background = element_rect(colour = "white"),
    strip.text = element_text(size = 16),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank())



